<?php
require_once dirname(__FILE__) . '/../includes/common.inc.php';
require_once dirname(__FILE__) . '/../includes/pdoorm.inc.php';

header('Content-Type: text/plain');


$action = MiscUtils::getParam('action', NULL);

try {
  switch ($action) {
    case 'login':
      login();
      break;
      
    case 'customer':
      customerquery($action);
      break;  

    case 'logout':
      logout();
      break;
      
    case 'save':
        {
            savesign($action);
        }
      break;

    case 'customerquery':
        {
            cquery($action);
        }
      break;      

    case 'password':
        {
            updatepwd($action);
        }
      break;      
            
      
    default:
      break;
  }
} catch (PDOException $e) {
  echo $e->getMessage();
}



function login()
{
	session_start();
	$password = MiscUtils::getParam('password','');
	
	  $sqlo = "select * from ns_ipadlogin where password = '".$password."' limit 1";
   	  $orderinfo = getone($sqlo);
	
	if(!empty($orderinfo->e_oid ))
	{
		$_SESSION['pwd'] = $password;
		setcookie(pwd,$password, (time() + 36000), PATH, DOMAIN);
		
		echo 1;
	}else{
		echo 2;
		
	}
	
}

function logout()
{
	session_start();
  		//$_SESSION['pwd'] = '';
  		
	    unset($_SESSION['pwd']);
	    session_destroy();
		setcookie(pwd,'', (time() - 1), PATH, DOMAIN);
		
		return ;
}


function customerquery()
{
  //$oid = MiscUtils::getParam('customerid', 892);
  $orderid = MiscUtils::getParam('orderid', '');
  
  
   $result = new stdClass();
   $result->data = array();
   $tmpaf = array();

   if(!empty($orderid))
   {
   	  $sqlo = "select * from ns_v1myorder where e_oid = ".$orderid." limit 1";
   	  $orderinfo = getone($sqlo);
   	 
   	
   	  $sql = "select * from ns_v1customer where e_oid =".$orderinfo->customerId." ";
   	  
   	  $cusinfo = getone($sql);
   	  
   	  $tmpaforders = array();
   	  
   	  $tmporder = getsonall('ns_v1myorder','e_oid',$orderid);
   	  
      foreach($tmporder as $order)
   	  {
   	      $order->products = array();
          $pitem = getsonall('ns_v1purchaseitem','order_id',$order->e_oid);
          
          $order->products = $pitem;
          
          $order->payments = array();
          $payment = getsonall('ns_v1payment','orderId',$order->e_oid); 
          $order->payments = $payment;
          
          $order->contract = array();
          
          $contract = getsonall('ns_contract','orderType',$order->type);
          $order->contract = $contract;

          $order->sign = array();
          $sign = getsonall('ns_signatures','orderid',$order->e_oid);
          $order->sign = $sign;          
          
          
          $tmpaforders[] = $order;
   	  }
   	  
   	  
   	  $tmpaf['bridename'] = $cusinfo->bridename;
   	  $tmpaf['bridephone'] = $cusinfo->bridephone;
   	  $tmpaf['bridemobile'] = $cusinfo->bridemobile;
   	  $tmpaf['brideemail'] = $cusinfo->brideemail;
   	  $tmpaf['brideaddrLn1'] = $cusinfo->brideaddrLn1;
   	  $tmpaf['brideaddrLn2'] = $cusinfo->brideaddrLn2;
   	  $tmpaf['brideaddrLn3'] = $cusinfo->brideaddrLn3;
   	  $tmpaf['brideaddrLn4'] = $cusinfo->brideaddrLn4;
   	  
   	  $tmpaf['groomname'] = $cusinfo->groomname;
   	  $tmpaf['groomphone'] = $cusinfo->groomphone;
   	  $tmpaf['groommobile'] = $cusinfo->groommobile;
   	  $tmpaf['groomemail'] = $cusinfo->groomemail;
   	  $tmpaf['groomaddrLn1'] = $cusinfo->groomaddrLn1;
   	  $tmpaf['groomaddrLn2'] = $cusinfo->groomaddrLn2;
   	  $tmpaf['groomaddrLn3'] = $cusinfo->groomaddrLn3;
   	  $tmpaf['groomaddrLn4'] = $cusinfo->groomaddrLn4;
   	  $tmpaf['customerid'] = $cusinfo->e_oid;
   	  
   	  $tmpaf['orders'] = $tmpaforders;
   	  
   	  
   } 
   $result->data[] =  $tmpaf;
  
  

  
  echo json_encode($result);	
	
	
}


function cquery()
{
  $oid = MiscUtils::getParam('customerid', '');
  //$orderid = MiscUtils::getParam('orderid', 1045);
  
  
   $result = new stdClass();
   $result->data = array();
   $tmpaf = array();

   if(!empty($oid))
   {
   	  /*$sqlo = "select * from ns_v1myorder where e_oid = ".$orderid." limit 1";
   	  $orderinfo = getone($sqlo);*/
   	 
   	
   	  $sql = "select * from ns_v1customer where e_oid =".$oid." ";
   	  
   	  $cusinfo = getone($sql);
   	  
   	  $tmpaforders = array();
   	  
   	  $tmporder = getsonall('ns_v1myorder','customerId',$oid);
   	  
      foreach($tmporder as $order)
   	  {
   	      $order->products = array();
          $pitem = getsonall('ns_v1purchaseitem','order_id',$order->e_oid);
          
          $order->products = $pitem;
          
          $order->payments = array();
          $payment = getsonall('ns_v1payment','orderId',$order->e_oid); 
          $order->payments = $payment;
          
          $order->contract = array();
          
          $contract = getsonall('ns_contract','orderType',$order->type);
          $order->contract = $contract;

          $order->sign = array();
          $sign = getsonall('ns_signatures','orderid',$order->e_oid);
          $order->sign = $sign;          
          
          
          $tmpaforders[] = $order;
   	  }
   	  
   	  
   	  $tmpaf['bridename'] = $cusinfo->bridename;
   	  $tmpaf['bridephone'] = $cusinfo->bridephone;
   	  $tmpaf['bridemobile'] = $cusinfo->bridemobile;
   	  $tmpaf['brideemail'] = $cusinfo->brideemail;
   	  $tmpaf['brideaddrLn1'] = $cusinfo->brideaddrLn1;
   	  $tmpaf['brideaddrLn2'] = $cusinfo->brideaddrLn2;
   	  $tmpaf['brideaddrLn3'] = $cusinfo->brideaddrLn3;
   	  $tmpaf['brideaddrLn4'] = $cusinfo->brideaddrLn4;
   	  
   	  $tmpaf['groomname'] = $cusinfo->groomname;
   	  $tmpaf['groomphone'] = $cusinfo->groomphone;
   	  $tmpaf['groommobile'] = $cusinfo->groommobile;
   	  $tmpaf['groomemail'] = $cusinfo->groomemail;
   	  $tmpaf['groomaddrLn1'] = $cusinfo->groomaddrLn1;
   	  $tmpaf['groomaddrLn2'] = $cusinfo->groomaddrLn2;
   	  $tmpaf['groomaddrLn3'] = $cusinfo->groomaddrLn3;
   	  $tmpaf['groomaddrLn4'] = $cusinfo->groomaddrLn4;
   	  $tmpaf['customerid'] = $cusinfo->e_oid;
   	  
   	  $tmpaf['orders'] = $tmpaforders;
   	  
   	  
   } 
   $result->data[] =  $tmpaf;
  
  

  
  echo json_encode($result);	
	
	
}

function savesign()
{
	$orderid = MiscUtils::getParam('orderid','');
	$output = MiscUtils::getParam('output','');
	$time = time();
	
	$addsql =" insert into ns_signatures (e_oid,signature,orderid,createdDate)
	                         values(NULL,'".$output."','".$orderid."','".$time."')";
	
    sql_execute($addsql);

   $result = new stdClass();
   $result->data = array();    
    
    $sql = "select * from ns_signatures where orderid = ".$orderid." limit 1";
    $orderinfo = getone($sql);
    
    $tmpaforders = array();
   	$tmpaf['signature'] = $orderinfo->signature;
   	$tmpaf['createdDate'] = $orderinfo->createdDate;
   	$tmpaf['orderid'] = $orderinfo->orderid;

   	$result->data[] =  $tmpaf;
  
    echo json_encode($result);

}


function updatepwd()
{
	
	$password = MiscUtils::getParam('password','');
	
	 $sql1 = "update ns_ipadlogin set password = '".$password."'";
	
	 sql_execute($sql1);
	
	  $sqlo = "select * from ns_ipadlogin where password = '".$password."' limit 1";
   	  $orderinfo = getone($sqlo);
	
	if(!empty($orderinfo->e_oid ))
	{
		$_SESSION['pwd'] = $password;
		setcookie(pwd, $password, (time() + 36000), PATH, DOMAIN);
		
		echo 1;
	}else{
		echo 2;
		
	}
	
}


?>